package com.newsmanagement.controller.backstage;

import com.newsmanagement.common.Constants;
import com.newsmanagement.entity.NewsContentEntity;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IFileService;
import com.newsmanagement.service.INewsCategoryService;
import com.newsmanagement.service.INewsContentService;
import com.newsmanagement.util.CheckAndChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @author xbr
 * 2017.12.04 11:24
 * 后台系统 新闻内容管理
 */
@Controller
@RequestMapping("newsContentBack")
public class NewsContentBackstageController {

    @Autowired
    private INewsContentService newsContentService;

    @Autowired
    private IFileService fileService;

    /**
     * 添加新闻内容
     * @param contentTitle
     * @param contentTxt
     * @param categoryId
     * @returnhttp://localhost:8080/newsContentBack/add?contentTitle=title&contentTxt=txt&categoryId=1
     */
    @RequestMapping("add")
    @ResponseBody
    public ReturnedMessages addNewsContent(
            @RequestParam( "contentTitle" ) String contentTitle,
            @RequestParam( "contentTxt" ) String contentTxt,
            @RequestParam( "categoryId" ) Integer categoryId
    ){

        return newsContentService.addNewsContent( contentTitle ,contentTxt ,categoryId);
    }

    /**
     * 图片上传
     * @param file 上传图片
     */
    @RequestMapping("uploadFile")
    @ResponseBody
    public ReturnedMessages uploadFile(
            HttpServletRequest request ,
            @RequestParam( "file" ) MultipartFile file
    ){
        return fileService.uploadFile(request ,file , Constants.FILE_PATH);
    }

    /**
     * 修改新闻内容
     * @param newsContent 新闻内容
     * @return
     * http://localhost:8080/newsContentBack/update?contentId=1&contentTitle=xxx&contentTxt=reg&categoryId=10
     */
    @RequestMapping("update")
    @ResponseBody
    public ReturnedMessages updateNewsContent(
            @ModelAttribute( "newsContent" ) NewsContentEntity newsContent
    ){

        System.out.println("newsContent:" + newsContent.toString());
        return newsContentService.updateNewsContent(newsContent);
    }

    /**
     * 删除新闻内容
     * @param contentId 新闻内容id
     * @return
     * http://localhost:8080/newsContentBack/delete?contentId=1
     */
    @RequestMapping("delete")
    @ResponseBody
    public ReturnedMessages deleteNewsContent(
            @RequestParam( "contentId" ) Integer contentId
    ){

        System.out.println("contentId:" + contentId);
        return newsContentService.deleteNewsContent(contentId);
    }

    /**
     * 分页查询新闻列表
     * @param jumpPage 跳转页面
     * @param showSize 每页显示的数据条数
     * @return ReturnedMessages
     * http://localhost:8080/newsContentBack/list?jumpPage=1&showSize=2
     */
    @RequestMapping("list")
    @ResponseBody
    public ReturnedMessages newsContentList(
            @RequestParam(value = "jumpPage") Integer jumpPage ,
            @RequestParam(value = "showSize") Integer showSize
    ){

        return newsContentService.newsContentList(jumpPage ,showSize);
    }


}
